<?php

namespace App\Utils\Helper;

use App\Utils\Annotation\ApiAuthority;
use App\Utils\Annotation\ApiParam;

class ApiHelper extends BaseHelper {

    /**
     * 权限标识
     * @var ApiAuthority
     */
    public static $authority;

    /**
     * 声明参数
     * @var array
     */
    public static $parameters = [];

    /**
     * 设置权限
     * @param ApiAuthority $authority
     */
    public function setAuthority (ApiAuthority $authority) {
        self::$authority = $authority;
    }

    /**
     * 获取权限
     * @return ApiAuthority
     */
    public function getAuthority () {
        return self::$authority;
    }

    /**
     * 设置参数
     * @param ApiParam $param
     */
    public function setParameter (ApiParam $param) {
        array_push(self::$parameters, $param);
    }

    /**
     * 获取参数
     * @return array
     */
    public function getParameters (): array {
        return self::$parameters;
    }

    /**
     * 获取当前登录用户
     * @return string|\Stringable|\Symfony\Component\Security\Core\User\UserInterface|null
     */
    public static function getAdminUser () {
        $result = null;

        $container = GetterHelper::getContainer();
        if ($container->has('security.token_storage')) {
            $token = $container->get('security.token_storage')->getToken();
            if (!is_null($token)) {
                $result = $token->getUser();
            }
        }

        return $result;
    }

    /**
     * 获取当前访问下的控制器域
     * @param $_controller
     * @return mixed|null
     */
    public static function getControllerDomain ($_controller) {
        $controller_domain = null;

        $parts = explode('\\', $_controller);
        if (isset($parts[2])) {
            $controller_domain = $parts[2];
        }

        return $controller_domain;
    }
}